<?php

	// +----------------------------------------------------------------------
	// | 站内消息
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\ucenter\admin;

	use app\controller\admin\Common;
	use app\model\ucenter\User as UserModel;
	use yicmf\builder\Edit;
	use yicmf\builder\Table;
	use app\event\ucenter\Notice as NoticeEvent;
	use app\model\ucenter\EmailHistory as EmailHistoryModel;
	use app\model\ucenter\UserGrade as UserGradeModel;
	use app\model\ucenter\EmailTemplate as EmailTemplateModel;

	class  EmailHistory extends Common
	{
		protected function initialize()
		{
			parent::initialize();
		}


		public function _filter()
		{
			$map = [];
			$map[] = ['status', '>', -2];
			return $map;
		}

		public function index()
		{

			// 读取规则列表
			// 显示页面
			$statusOptions = [
				[
					'id' => 0,
					'value' => '未发送',
				],
				[
					'id' => 1,
					'value' => '已发送',
				]
			];
			// 显示页面
			$builder = new Table();
			$builder->title('邮件记录')
				->model(EmailHistoryModel::class)
				->where($this->_filter())
				->order('id DESC')
//				->buttonUpdate('send', '发送邮件')
				->buttonCustom('ucenter/admin/Email/index','邮件地址管理')
				->searchText('address', '发送地址')
				->searchTextLike('body', '发送内容')
				->searchSelect('status', '发送状态：', $statusOptions)
				->keyId()
				->keyText('template.title', '模版名称', false, 120)
				->keyText('address', '收件地址', false, 180)
				->keyText('subject', '发送标题', false, 180)
//				->keyText('{$data.content|strip_tags}', '发送内容')
				->keyText('result_message', '结果')
                ->keyIp()
				->keyTime('send_time', '发送时间')
				->keyCreateTime();
//				->actionDelete();

			return $builder->fetch();
		}

		/**
		 * 新增消息
		 */
		public function send()
		{
			if ($this->request->isPost()) {
				$result = NoticeEvent::sendByAdmin($this->request->post(), $this->user);
				if (0 == $result['code']) {
					$this->_returnSuccess( $result['message'],false,false);
				} else {
					$this->_returnError( '发送消息失败，失败信息【' . $result['message'] . '】');
				}
			} else {
				$data = [];//
				$users = UserModel::where('status', 1)->count();
				$template = EmailTemplateModel::where('status', 1)->where('id', 6)->column('title', 'id');
				$builder = new Edit();
				$builder->title('发送邮件通知')
					->keyTextInline('title', '标题')
					->keyEditor('content', '内容')
//					->keyCheckBox('type', '接收方式', ['站内信', 'APP推送','邮件', '微信公众号', '短信'])
					->keyCheckBox('types', '接收方式', [ 'email' => '邮件'],'',['email'])
					->keyRadio('to_type', '发送方式', ['指定用户ID', '指定手机号', '指定邮箱', '卖家组', 'VIP卖家', '全部'])
					->keyRadio('email_template_id', '选择模板', $template, '', 6)
					->keyRadio('user_grade_id', '用户组', UserGradeModel::where('status', 1)->column('title', 'id'), '', 1)
					->keyNumber('user_id', '用户ID')
					->keyEmail('email', '邮件地址');
				if ($users > 10000) {
					$tips = '单次发送用户数最多10000个，当前有效用户' . $users . '个，需要分批发送，默认从第一批';
					$builder->keyNumber('user_page', '用户分页', $tips, 1);
				}else{
					$builder->keyHidden('user_page', 1);
				}
				$builder
					->setTrigger('to_type', 0, 'user_id')
					->setTrigger('to_type', 1, 'mobile')
					->setTrigger('to_type', 2, 'email,template_id')
					->setTrigger('to_type', 3, 'user_grade_id,user_page')
					->setTrigger('to_type', '4,5', 'user_page')
					->data($data)
					->buttonSubmit();
				return $builder->fetch();
			}
		}
		/**
		 * 删除消息
		 */
		public function delete()
		{
			$message = EmailHistoryModel::where('id', $this->request->param('id'))->find();
			if ($message) {
				$message['status'] = -2;
				$message->save();
				$this->_returnSuccess( '删除成功');
			} else {
				$this->_returnSuccess( '操作失败');
			}
		}

	}
